package com.yulinlin.websocket.node.handle;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.UUID;

@Slf4j
@Component
public class MessageEventHandler {

    public  SocketIOServer socketIoServer;

    public MessageEventHandler(SocketIOServer server) {
        this.socketIoServer = server;
    }

    @OnConnect
    public void onConnect(SocketIOClient client) {
        UUID socketSessionId = client.getSessionId();
        String ip = client.getRemoteAddress().toString();

        log.info(ip+"链接");
        socketIoServer.getRoomOperations("msg")
                .sendEvent("msg","链接成功");


    }

    @OnEvent("joinRoom")
    public void joinRoom(SocketIOClient client, AckRequest request, String data) {
        UUID socketSessionId = client.getSessionId();
        String ip = client.getRemoteAddress().toString();
        //加入房间
        client.joinRoom(data);
        Set<String> rooms = client.getAllRooms();
        socketIoServer.getRoomOperations("msg")
                .sendEvent("msg","加入房间");

    }

    @OnEvent("send")
    public void send(SocketIOClient client, AckRequest request, String data) {
        socketIoServer.getRoomOperations("msg")
                .sendEvent("msg","事件广播");


    }



    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        UUID socketSessionId = client.getSessionId();
        String ip = client.getRemoteAddress().toString();
        log.info(ip+"离开");
    }

}
